/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package freewolf;

import com.puzzud.engine.core.Item2D;
import com.puzzud.engine.video.VideoDisplay;
import com.puzzud.engine.video.VideoSprite;

/**
 *
 * @author puzzud
 */
public class Text extends Item2D
{
    public final static int ALIGNMENT_LEFT  = 0;
    public final static int ALIGNMENT_RIGHT = 1;
    public final static int ALIGNMENT_DOWN  = 2;

    public final static int ASCII[] =
    {
        BlockGraphic.INDEX_EMPTY,                   // 0 Null
        BlockGraphic.INDEX_BODY_HEAD_NAZI_FRONT,    // 1
        BlockGraphic.INDEX_BODY_HEAD_ALLIE_FRONT,   // 2
        BlockGraphic.INDEX_EMPTY,                   // 3 Heart
        BlockGraphic.INDEX_EMPTY,                   // 4 Diamond
        BlockGraphic.INDEX_EMPTY,                   // 5 Club
        BlockGraphic.INDEX_EMPTY,                   // 6 Spade
        BlockGraphic.INDEX_EMPTY,                   // 7 Circle
        BlockGraphic.INDEX_EMPTY,                   // 8 Inverted Circle
        BlockGraphic.INDEX_EMPTY,                   // 9 Tab
        BlockGraphic.INDEX_EMPTY,                   // 10 lf?
        BlockGraphic.INDEX_EMPTY,                   // 11 Male Symbol
        BlockGraphic.INDEX_EMPTY,                   // 12 Female Symbole
        BlockGraphic.INDEX_EMPTY,                   // 13 Carriage Return
        BlockGraphic.INDEX_EMPTY,                   // 14 Music Notes
        BlockGraphic.INDEX_EMPTY,                   // 15 Star
        BlockGraphic.INDEX_EMPTY,                   // 16 Right Arrow
        BlockGraphic.INDEX_LEFT_ARROW,              // 17 Left Arrow
        BlockGraphic.INDEX_EMPTY,                   // 19 Up/Down Arrows
        BlockGraphic.INDEX_EMPTY,                   // 20 Double Exclamation
        BlockGraphic.INDEX_EMPTY,                   // 21 Indent
        BlockGraphic.INDEX_EMPTY,                   // 22 nak?
        BlockGraphic.INDEX_EMPTY,                   // 23 etb?
        BlockGraphic.INDEX_EMPTY,                   // 24 Up Arrow
        BlockGraphic.INDEX_EMPTY,                   // 25 Down Arrow
        BlockGraphic.INDEX_EMPTY,                   // 26 eof
        BlockGraphic.INDEX_LEFT_ARROW,              // 27 Escape (Tilde)
        BlockGraphic.INDEX_EMPTY,                   // 28 Lower Right Corner
        BlockGraphic.INDEX_EMPTY,                   // 29 Left/Right Arrow
        BlockGraphic.INDEX_EMPTY,                   // 30 Up Arrow
        BlockGraphic.INDEX_EMPTY,                   // 31 Down Arrow
        BlockGraphic.INDEX_EMPTY,                   // 32 Space
        BlockGraphic.INDEX_EXCLAMATION,             // 33 Exclamation
        BlockGraphic.INDEX_QUOTE,                   // 34 Quote
        BlockGraphic.INDEX_POUND,                   // 35 Pound
        BlockGraphic.INDEX_DOLLAR,                  // 36 Dollar
        BlockGraphic.INDEX_PERCENT,                 // 37 Percent
        BlockGraphic.INDEX_AMPERSAND,               // 38 Ampersand
        BlockGraphic.INDEX_APOSTROPHE,              // 39 Apostrophe
        BlockGraphic.INDEX_OPEN_PARANTHESES,        // 40 Down Arrow
        BlockGraphic.INDEX_CLOSE_PARANTHESES,       // 41 Down Arrow
        BlockGraphic.INDEX_ASTERIX,                 // 42 Down Arrow
        BlockGraphic.INDEX_PLUS,                    // 43 Down Arrow
        BlockGraphic.INDEX_COMMA,                   // 44 Down Arrow
        BlockGraphic.INDEX_HYPHEN,                  // 45 Down Arrow
        BlockGraphic.INDEX_PERIOD,                  // 46 Down Arrow
        BlockGraphic.INDEX_SLASH,                   // 47 Down Arrow
        BlockGraphic.INDEX_0,                       // 48 Down Arrow
        BlockGraphic.INDEX_1,                       // 49 Down Arrow
        BlockGraphic.INDEX_2,                       // 50 Down Arrow
        BlockGraphic.INDEX_3,                       // 51 Down Arrow
        BlockGraphic.INDEX_4,                       // 52 Down Arrow
        BlockGraphic.INDEX_5,                       // 53 Down Arrow
        BlockGraphic.INDEX_6,                       // 54 Down Arrow
        BlockGraphic.INDEX_7,                       // 55 Down Arrow
        BlockGraphic.INDEX_8,                       // 56 Down Arrow
        BlockGraphic.INDEX_9,                       // 57 Down Arrow
        BlockGraphic.INDEX_COLON,                   // 58 Down Arrow
        BlockGraphic.INDEX_SEMICOLON,               // 59 Down Arrow
        BlockGraphic.INDEX_LESS_THAN,               // 60 Down Arrow
        BlockGraphic.INDEX_EQUAL,                   // 61 Down Arrow
        BlockGraphic.INDEX_GREATER_THAN,            // 62 Down Arrow
        BlockGraphic.INDEX_QUESTION,                // 63 Down Arrow
        BlockGraphic.INDEX_ASPERAND,                // 64 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_A,             // 65 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_B,             // 66 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_C,             // 67 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_D,             // 68 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_E,             // 69 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_F,             // 70 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_G,             // 71 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_H,             // 72 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_I,             // 73 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_J,             // 74 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_K,             // 75 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_L,             // 76 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_M,             // 77 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_N,             // 78 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_O,             // 79 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_P,             // 80 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_Q,             // 81 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_R,             // 82 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_S,             // 83 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_T,             // 84 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_U,             // 85 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_V,             // 86 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_W,             // 87 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_X,             // 88 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_Y,             // 89 Down Arrow
        BlockGraphic.INDEX_UPPERCASE_Z,             // 90 Down Arrow
        BlockGraphic.INDEX_OPEN_BRACKET,            // 91 Down Arrow
        BlockGraphic.INDEX_BODY_ARM_UP_LEFT,        // 92 TODO: Black Slash!
        BlockGraphic.INDEX_CLOSE_BRACKET,           // 93 Down Arrow
        BlockGraphic.INDEX_UP_ARROW,                // 94 TODO: Power Symbol?
        BlockGraphic.INDEX_UNDERSCORE,              // 95 Down Arrow
        BlockGraphic.INDEX_UNKNOWN_119,             // 96 TODO: Opposite Single Quote?
        BlockGraphic.INDEX_LOWERCASE_A,             // 97 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_B,             // 98 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_C,             // 99 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_D,             // 100 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_E,             // 101 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_F,             // 102 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_G,             // 103 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_H,             // 104 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_I,             // 105 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_J,             // 106 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_K,             // 107 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_L,             // 108 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_M,             // 109 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_N,             // 110 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_O,             // 111 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_P,             // 112 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_Q,             // 113 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_R,             // 114 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_S,             // 115 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_T,             // 116 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_U,             // 117 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_V,             // 118 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_W,             // 119 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_X,             // 120 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_Y,             // 121 Down Arrow
        BlockGraphic.INDEX_LOWERCASE_Z,             // 122 Down Arrow
        BlockGraphic.INDEX_MOON_LEFT,               // 123 TODO: Left Curly Brace
        BlockGraphic.INDEX_VERTICAL_BAR,            // 124 Down Arrow
        BlockGraphic.INDEX_MOON_RIGHT,              // 125 TODO: Right Curly Brace
        BlockGraphic.INDEX_BAT,                     // 126 TODO: Tilde
        BlockGraphic.INDEX_MOON_TOP,                // 127 TODO: Delta Triangle
    };

    VideoDisplay videoDisplay;
    VideoSprite blockGraphicsSpriteList[];

    String string;
    int alignment;
    float[] color;
    float alpha;

    public Text( VideoDisplay videoDisplay, VideoSprite blockGraphicsSpriteList[] )
    {
        this.videoDisplay = videoDisplay;
        this.blockGraphicsSpriteList = blockGraphicsSpriteList;

        setAlignment( ALIGNMENT_LEFT );
        setString( "" );
        setColor( new float[]{1.0f, 1.0f, 1.0f} );
        setAlpha( 1.0f );
    }
    
    public void print()
    {
        int index;
        int charCode;

        int x, y;
        
        if( alignment == ALIGNMENT_LEFT )
        {
            y = getYPosition() * BlockGraphic.HEIGHT;

            for( index = 0; index < string.length(); index++ )
            {
                // TODO: This could be optimized with drawBlock?
                x = ( getXPosition() + index ) * BlockGraphic.WIDTH;

                charCode = string.charAt( index );
                
                // Bound this character code, in case it's not handled.
                if( charCode < 0 )
                {
                    charCode = 0;
                }
                else
                if( charCode >= ASCII.length )
                {
                    charCode = 0;
                }

                charCode = ASCII[charCode] - 1; // NOTE: Why do I have to subtract 1?

                blockGraphicsSpriteList[charCode].setPosition( x, y );
                blockGraphicsSpriteList[charCode].setColor( color );
                blockGraphicsSpriteList[charCode].setAlpha( alpha );

                videoDisplay.draw( blockGraphicsSpriteList[charCode] );
            }
        }/*
        else
        if( alignment == ALIGNMENT_RIGHT )
        {
            x = x - length * printCharWidth;

            for( index = 0; index < length; index++ )
            {
                CharGfx.SetCurrentFrame( ( int )( text[index] ) - 32 );

                CharGfx.Draw( &Display,
                               x, y,
                               0, 0,
                               charWidth, charHeight,
                               printCharWidth, printCharHeight );

                x = x + printCharWidth;
            }
        }
        else
        if( alignment == ALIGNMENT_DOWN )
        {
            for( index = 0; index < length; index++ )
            {
                CharGfx.SetCurrentFrame( ( int )( text[index] ) - 32 );

                CharGfx.Draw( &Display,
                               x, y,
                               0, 0,
                               charWidth, charHeight,
                               printCharWidth, printCharHeight );

                y = y + printCharHeight;
            }
        }*/
    }

    public String getString()
    {
        return( string );
    }

    public void setString( String string )
    {
        this.string = string;
    }

    public int getAlignment()
    {
        return( alignment );
    }

    public void setAlignment( int alignment )
    {
        this.alignment = alignment;
    }

    public float[] getColor()
    {
        return( color );
    }

    public void setColor( float[] color )
    {
        if( color.length > 2 )
        {
            this.color = color;
        }
    }

    public float getAlpha()
    {
        return( alpha );
    }

    public void setAlpha( float alpha )
    {
        this.alpha = alpha;
    }


}
